# -*- coding: utf-8 -*-
"""
Created on Wed May  2 03:20:55 2018

@author: home
"""

import numpy as np
import pandas as pd

from sklearn.metrics import r2_score
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
from sklearn.linear_model import RidgeCV
from sklearn.linear_model import LassoCV
import matplotlib.pyplot as plt
import seaborn as sns

data = pd.read_csv("day.csv")

#数据基本情况查看
#print(data.head(3))
#print(data.shape)
#print(data.info())
#print(data.isnull().sum())#判断是否有空值情况
#print(data.describe(include='all'))#查看数据的冗余情况

#1、根据日期对dataframe进行分割
data2011 = data[(data.dteday>='2011-01-01') & (data.dteday <= '2012')]
data2012 = data[(data.dteday >= '2012')]


#print(data2011[['temp','atemp']])
print("============================================")
#print(data2012[['temp','atemp']])


#2、数据特征分析

#单变量分析
fig = plt.figure()
sns.distplot(data2011.temp.values, bins=30, kde=False)
plt.xlabel("temp", fontsize=12)
plt.show()

fig = plt.figure()
sns.distplot(data2011.atemp.values, bins=30, kde=False)
plt.xlabel("atemp", fontsize=12)
plt.show()

fig = plt.figure()
sns.distplot(data2011.hum.values, bins=30, kde=False)
plt.xlabel("hum", fontsize=12)
plt.show()

fig = plt.figure()
sns.distplot(data2011.windspeed.values, bins=30, kde=False)
plt.xlabel("windspeed", fontsize=12)
plt.show()

#噪声排除
print(data2011.shape)
data2011=data2011[(data2011.windspeed<0.5)&(data2011.hum>0.1)]
print(data2011.shape)

# 查看天气分布情况
plt.scatter(range(data2011.shape[0]), data2011["weathersit"].values,color='red')
plt.title("weatherSit");
plt.show()



cols = data2011.columns
data_corr = data2011.corr().abs()
print(data_corr.shape)

plt.subplots(figsize=(13, 9))
sns.heatmap(data_corr,annot=True)

# 比对各个特征值的相关性
plt.savefig('day_corr.png')
plt.show()

#舍弃一些不相干的数据
data_useful=data2011.drop(['instant','yr','season','dteday','weekday','weathersit','atemp','casual','registered'], axis=1)
test_date_useful=data2012.drop(['instant','yr','season','dteday','weekday','weathersit','atemp','casual','registered'], axis=1)
#print(data_useful.shape)
#print(data_useful.head(2))

y_train=data_useful['cnt'].values
x_train=data_useful.drop('cnt', axis=1)
y_test=test_date_useful['cnt'].values
x_test=test_date_useful.drop('cnt', axis=1)

print("========================================+++++++++++++++++++++++++++++++++++++++++++++")
print(x_train)

#3、数据进行归一化处理
x_train_scale=preprocessing.scale(x_train, axis=1, with_mean=False, with_std=False )
data_train=pd.DataFrame(x_train_scale, columns=x_train.columns.values)
print("--------------------------------------------")
#print(data.head(20))
#print(data.shape)

x_test_scale=preprocessing.scale(x_test, axis=1, with_mean=False, with_std=False)
data_test=pd.DataFrame(x_test_scale, columns=x_test.columns.values)
print("--------------------------------------------")
#print(data.head(20))
#print(data.shape)

diff=np.mean(y_test)-np.mean(y_train)
print("diff======================",diff)
needadd = np.ones(y_test_pred.shape[0])*diff

###############领回归
alphas = [0.01, 0.1, 1, 10, 100]
ridge = RidgeCV(alphas=alphas, store_cv_values=True)
ridge.fit(data_train, y_train)

y_test_pred = ridge.predict(data_test)
y_train_pred = ridge.predict(data_train)


print("r2 score on test is ", r2_score(y_test, y_test_pred+needadd))
print("r2 score on train is ", r2_score(y_train, y_train_pred))

mse_mean = np.mean(ridge.cv_values_, axis = 0)
plt.plot(np.log10(alphas), mse_mean.reshape(len(alphas),1)) 

plt.xlabel('log(alpha)')
plt.ylabel('mse')
plt.show()


#############lasso回归
lasso = LassoCV()  
lasso.fit(data_train, y_train)  
y_test_pred_lasso = lasso.predict(data_test)
y_train_pred_lasso = lasso.predict(data_train)


# 评估，使用r2_score评价模型在测试集和训练集上的性能
print ('The r2 score of LassoCV on test is', r2_score(y_test, y_test_pred_lasso+needadd))
print ('The r2 score of LassoCV on train is', r2_score(y_train, y_train_pred_lasso))






